package cn.endeavour.pet.controller;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.pet.domain.Pet;
import cn.endeavour.pet.domain.PetDetail;
import cn.endeavour.pet.domain.SearchMasterMsg;
import cn.endeavour.pet.query.SearchMasterMsgQuery;
import cn.endeavour.pet.service.IPetDetailService;
import cn.endeavour.pet.service.ISearchMasterMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 宠物寻主接口
 */
@Api(tags = "寻主接口",description = "寻主接口详细描述")
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IPetDetailService detailService;

    /**
     * 处理宠物消息
     * @param pet 传入的宠物参数
     * @return 处理的结果
     */
    @PutMapping("/handle")
    public AjaxResult handler(@RequestBody Pet pet, HttpServletRequest request){
        try {
            searchMasterMsgService.handle(pet, request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，稍后重试!!!");
        }
    }

    /**
     * 查询宠物细节接口
     * @param id 宠物id
     * @return 宠物细节
     */
    @GetMapping("/detail/{id}")
    public PetDetail findDetail(@PathVariable("id") Long id) {
        return detailService.findDetailByMsgId(id);
    }

    /**
     * 已经处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return searchMasterMsgService.processed(query, request);
    }

    /**
     * 用户
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return searchMasterMsgService.user(query, request);
    }

    /**
     * 待处理信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return searchMasterMsgService.pending(query, request);
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg 寻主消息
     * @param request 请求
     * @return
     */
    @ApiOperation(value = "发布寻主消息",notes = "")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，稍后重试!!!");
        }
    }

    /**
     * 查询所有
     * @return 所有内容
     */
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.getAll();
    }

}
